package com.lineage.server.serverpackets;

import com.lineage.config.ConfigOther;
import com.lineage.config.ConfigRate;
import com.lineage.server.datatables.ShopTable;
import com.lineage.server.model.Instance.L1ItemStatus;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.shop.L1Shop;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1ShopItem;
import com.lineage.server.world.World;
import java.util.List;

public class S_ShopSellList extends ServerBasePacket {
  private byte[] _byte;
  
  public S_ShopSellList(int objId) {
    this._byte = null;
    writeC(70);
    writeD(objId);
    L1Object npcObj = World.get().findObject(objId);
    if (!(npcObj instanceof L1NpcInstance)) {
      writeH(0);
      return;
    } 
    int npcId = ((L1NpcInstance)npcObj).getNpcTemplate().get_npcId();
    L1Shop shop = ShopTable.get().get(npcId);
    List<?> shopItems = shop.getSellingItems();
    if (shopItems.size() <= 0) {
      writeH(0);
      return;
    } 
    writeH(shopItems.size());
    int i = 0;
    while (i < shopItems.size()) {
      L1ShopItem shopItem = (L1ShopItem)shopItems.get(i);
      L1Item item = shopItem.getItem();
      int price = (int)(shopItem.getPrice() * ConfigRate.RATE_SHOP_SELLING_PRICE);
      writeD(i);
      writeH(shopItem.getItem().getGfxId());
      writeD(price);
      int enchant = shopItem.getEnchantLevel();
      String s1 = "";
      if (enchant != 0)
        s1 = "+" + enchant + " "; 
      if (shopItem.getPackCount() > 1) {
        writeS(String.valueOf(String.valueOf(s1)) + item.getNameId() + " (" + shopItem.getPackCount() + ")");
      } else {
        writeS(String.valueOf(String.valueOf(s1)) + item.getNameId());
      } 
      if (ConfigOther.SHOPINFO) {
        L1ItemStatus itemInfo = new L1ItemStatus(item);
        byte[] status = itemInfo.getStatusBytes(true).getBytes();
        writeC(status.length);
        byte[] array;
        int length = (array = status).length;
        int j = 0;
        while (j < length) {
          byte b = array[j];
          writeC(b);
          j++;
        } 
      } else {
        writeC(0);
      } 
      i++;
    } 
    writeH(7);
  }
  
  public S_ShopSellList(L1NpcInstance npc) {
    this._byte = null;
    writeC(70);
    writeD(npc.getId());
    int npcId = npc.getNpcTemplate().get_npcId();
    L1Shop shop = ShopTable.get().get(npcId);
    List<?> shopItems = shop.getSellingItems();
    if (shopItems.size() <= 0) {
      writeH(0);
      return;
    } 
    writeH(shopItems.size());
    int i = 0;
    while (i < shopItems.size()) {
      L1ShopItem shopItem = (L1ShopItem)shopItems.get(i);
      L1Item item = shopItem.getItem();
      int price = shopItem.getPrice();
      writeD(i);
      writeH(shopItem.getItem().getGfxId());
      writeD(price);
      if (shopItem.getPackCount() > 1) {
        writeS(String.valueOf(String.valueOf(item.getNameId())) + " (" + shopItem.getPackCount() + ")");
      } else {
        writeS(item.getNameId());
      } 
      writeC(0);
      i++;
    } 
    writeH(7);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
